package phoenix.web.test_objects;

import org.testng.TestException;
import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.TestBaseWebExt;
import phoenix.core.data_models.TestUserDataModel;

/**
 * Class contains test methods for Index page
 */
public class BackendTests extends TestBaseWebExt
{
	public BackendTests(PhoenixManager p_phoenix)
	{
		super(p_phoenix);
	}

	/**
	 * Gets user data from backend
	 * @p_email User email
	 */
	public boolean getUserDataTest(String p_email)
	{
		try
		{
			db().addUserData(testUser());
			wd().wait(30); // Need some time for syncing data
			TestUserDataModel l_model = backend().searchPage().getUserDataByPOSTRequest(p_email);

			if (l_model.getId() != null && l_model.getUserKey() != null)
			{
				manager.updateTestUser(l_model); // Update model
				db().updateUserData(l_model); // Update database
			}
			else
			{
				throw new TestException("Can't get user data from backend");
			}
		}
		catch (Exception p_ex)
		{
			log().warn("Cannot get user data from backend " + p_ex.toString());
			return false;
		}
		finally
		{
			test().forErrors();
			return true;
		}
	}

	/**
	 * Sets paid status for user
	 */
	public void makeUserPaid(TestUserDataModel p_user)
	{
		try
		{
			backend().searchPage().isUserPaid(p_user.getId(), true);
			testUser().setMembershipStatus(1);
			backend().searchPage().verifyPayStatus(p_user);
			db().updateUserData(testUser());
		}
		catch (Exception p_ex)
		{
			log().error("Cannot make user as Paid " + p_ex.toString());
		}

		test().forErrors();
	}

	public void approvePhoto(TestUserDataModel p_userModel)
	{
		try
		{
			manager.backend().indexPage().login();
			wd().openURL(manager.config().BACKEND_URL_BASE + "/user/photos?user_id=" + p_userModel.getId());

			if (!manager.backend().photosPage().clickApprove())
			{
				manager.backend().photosPage().clickDecline();
				manager.backend().photosPage().clickApprove();
			}

			manager.backend().photosPage().waitForApprovedPhoto();
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("Cannot approved photo: " + p_ex);
		}
	}
}